package com.mt.grand.singleton.lazy;

import com.mt.grand.singleton.dto.SingletonInstance;

public class LazyOne {

    private static SingletonInstance singletonInstance;

    private volatile static SingletonInstance singletonInstance1;

    //非线程安全的单例
    public static SingletonInstance getInstance1(){
        if(singletonInstance==null){
            singletonInstance = new SingletonInstance("我是懒汉式单例");
        }
        return  singletonInstance;
    }

    //低效率的线程安全的单例
    public static synchronized SingletonInstance getInstance2(){
        if(singletonInstance==null){
            singletonInstance = new SingletonInstance("我是懒汉式单例");
        }
        return  singletonInstance;
    }

    //较高效率的线程安全的单例
    public static SingletonInstance getInstance3(){
        if(singletonInstance1==null){
            synchronized (SingletonInstance.class){
                if(singletonInstance1==null){
                    singletonInstance1 = new SingletonInstance("我是懒汉式单例");
                }
            }
        }
        return  singletonInstance1;
    }
}